package start.spring.basic.interceptor;

import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

/**
 * 
 * ****************************************************************************
 * 自定义fastjson序列化器
 *
 * @author(作者)：xuyongyun	
 * @date(创建日期)：2023年8月28日
 ******************************************************************************
 */
public class CusToStringSerializer implements ObjectSerializer {

	public static final CusToStringSerializer instance = new CusToStringSerializer();
	
	@Override
	public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features)
			throws IOException {
		SerializeWriter out = serializer.out;

        if (object == null) {
            out.writeNull();
            return;
        }else {
        	
        	if("java.math.BigDecimal".equals(object.getClass().getName())) {
        		out.writeString(((BigDecimal)object).toPlainString());
        	}else {
        		String strVal = object.toString();
                out.writeString(strVal);
        	}
        }
	}
}
